package cn.fl.project.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjPrdPaySchDConstant;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.dto.PrjPrdPaySchDDTO;
import cn.fl.project.service.IPrjBscInfoService;
import cn.fl.project.service.IPrjPrdPaySchDService;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.project.vo.PrjBscInfoVO;
import cn.fl.project.vo.PrjPrdPaySchDVO;
import cn.fl.project.vo.PrjPrdPaySchMVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目产品支付表主明细信息 控制层
 */
@Controller
@RequestMapping("/project/prjPrdPaySchD")
public class PrjPrdPaySchDController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PrjPrdPaySchDController.class);

    /**
     * PrjPrdPaySchD 服务层BEAN
     */
    @Autowired
    private IPrjPrdPaySchDService prjPrdPaySchDService;

    /**
     * 数据字典 服务层BEAN
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    /**
     * PrjPrdPaySchM 服务层BEAN
     */
    @Autowired
    private IPrjPrdPaySchMService prjPrdPaySchMService;
    
    /**
     * PrjBscInfoService 服务层BEAN
     */
    @Autowired
    private IPrjBscInfoService prjBscInfoService;
    /**
     * 到列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载PrjPrdPaySchD列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjPrdPaySchD/prjPrdPaySchDList");
        return mav;
    }

    /**
     * 到新增页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载PrjPrdPaySchD Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjPrdPaySchD/prjPrdPaySchDModify");
        return mav;
    }

    /**
     * 到修改页面
     */
    @RequestMapping(value = "/toModifyPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toModifyPage", "加载PrjPrdPaySchD Modify页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("project/prjPrdPaySchD/prjPrdPaySchDModify");
        this.prepare(request, mav);

        String id = request.getParameter("id");
        PrjPrdPaySchDVO prjPrdPaySchDVO = this.prjPrdPaySchDService.selectById(Long.valueOf(id));
        mav.addObject("prjPrdPaySchDVO", prjPrdPaySchDVO);
        return mav;
    }

    //加载数据字典数据、传值等 ？
    public void prepare(HttpServletRequest request, ModelAndView mav) {

    }

    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initPrjPrdPaySchDList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }

        if (StringUtils.isNotEmpty(currentPage)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        PrjPrdPaySchDQueryCondition qc = new PrjPrdPaySchDQueryCondition();
        //TODO 设置查询条件？
        String searchName = request.getParameter("demo.name");
        if (StringUtils.isNotEmpty(searchName)) {

        }
        page.setCondition(qc);

        BaseLogUtils.info(logger, "initPrjPrdPaySchDList", "初始化PrjPrdPaySchD列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.prjPrdPaySchDService.searchListPage(page);
        List<PrjPrdPaySchDVO> list = (List<PrjPrdPaySchDVO>) pager.getResultList();
        ListPageVO<PrjPrdPaySchDVO> listPage = new ListPageVO<PrjPrdPaySchDVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     *获取结清费用项
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getPrjPrdPaySchSettFeeList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getPrjPrdPaySchSettFeeList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String paySchId = request.getParameter("paySchId");
        String prjId = request.getParameter("prjId");
        if (StringUtils.isEmpty(paySchId)) {
            throw new BaseException("查询参数：支付表ID不能为空");
        }
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
     
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        PrjPrdPaySchDQueryCondition qc = new PrjPrdPaySchDQueryCondition();
        //qc.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
        List<String> feeClsTypList = new ArrayList<>();//项目费用分类
        feeClsTypList.add(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
        feeClsTypList.add(PrjQuatPrpsConstant.CODE_FIN_CLS_TYP_SETT);//借款相关费用项展示
        qc.setFeeClsCds(feeClsTypList);
        qc.setPayPd(Long.valueOf(PrjQuatPrpsConstant.CODE_PRJ_PRD_FEE_PD));//期次为0为费用项
        qc.setPaySchId(Long.valueOf(paySchId));
        if(StringUtils.isNotBlank(prjId)){
            qc.setPrjId(Long.valueOf(prjId));
        }
        page.setCondition(qc);
        BaseLogUtils.info(logger, "getPrjPrdPaySchSettFeeList", "初始化getPrjPrdPaySchSettFeeList列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.prjPrdPaySchDService.searchSettFeeListPage(page);
        List<PrjPrdPaySchMVO> list = (List<PrjPrdPaySchMVO>) pager.getResultList();
        ListPageVO<PrjPrdPaySchMVO> listPage = new ListPageVO<PrjPrdPaySchMVO>(list, pager.getTotalCount());
        return listPage;
    }
    
    /**
     *获取结清费用项
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/getPrjPrdPaySchDefSettFeeList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getPrjPrdPaySchDefSettFeeList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        String paySchId = request.getParameter("paySchId");
        if (StringUtils.isEmpty(paySchId)) {
            throw new BaseException("查询参数：支付表ID不能为空");
        }
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
     
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        PrjPrdPaySchDQueryCondition qc = new PrjPrdPaySchDQueryCondition();
        qc.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
        qc.setPayPd(Long.valueOf(PrjQuatPrpsConstant.CODE_PRJ_PRD_FEE_PD));//期次为0为费用项
        qc.setIsSysDef(BaseConstant.IS_YESNO_YES);
        qc.setPaySchId(Long.valueOf(paySchId));
        page.setCondition(qc);
        BaseLogUtils.info(logger, "getPrjPrdPaySchSettFeeList", "初始化getPrjPrdPaySchSettFeeList列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.prjPrdPaySchDService.searchListPage(page);
        List<PrjPrdPaySchMVO> list = (List<PrjPrdPaySchMVO>) pager.getResultList();
        ListPageVO<PrjPrdPaySchMVO> listPage = new ListPageVO<PrjPrdPaySchMVO>(list, pager.getTotalCount());
        return listPage;
    }
    /**
     * 删除数据
     */
    @RequestMapping(value = "/removeByIds.do", method = {RequestMethod.POST})
    @ResponseBody
    public DataResultVO removePrjPrdPaySchDByIds(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String ids = request.getParameter("ids");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        if (StringUtils.isEmpty(ids)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "removePrjPrdPaySchDByIds", "删除PrjPrdPaySchD数据,主键参数为空!", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        List<Long> idsList = new ArrayList<Long>();
        JSONArray idsArray = JSONArray.fromObject(ids);
        for (Object object : idsArray) {
            idsList.add(Long.valueOf(object.toString()));
        }

        try {
            boolean success = this.prjPrdPaySchDService.removeByIds(idsList);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
                BaseLogUtils.info(logger, "removePrjPrdPaySchDByIds", "删除PrjPrdPaySchD数据失败,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
            }
            BaseLogUtils.info(logger, "removePrjPrdPaySchDByIds", "删除PrjPrdPaySchD数据成功,ID:" + idsList.toString(), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "removePrjPrdPaySchDByIds", "删除PrjPrdPaySchD数据失败,ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "removePrjPrdPaySchDByIds", "删除PrjPrdPaySchD数据失败,系统错误.ID:" + idsList.toString() + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 得到一条记录
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getById.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object getPrjPrdPaySchDById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        String id = request.getParameter("id");
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        if (StringUtils.isEmpty(id)) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
            BaseLogUtils.info(logger, "getPrjPrdPaySchDById", "获取PrjPrdPaySchD数据,主键参数为空.", CurrentThreadContext.getCurrentUserName());
            return result;
        }

        try {
            PrjPrdPaySchDVO vo = this.prjPrdPaySchDService.selectById(Long.valueOf(id));
            if (null == vo) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
                BaseLogUtils.info(logger, "getPrjPrdPaySchDById", "获取PrjPrdPaySchD数据不存在,ID:" + id, CurrentThreadContext.getCurrentUserName());
            }
            result.setData(vo);
            BaseLogUtils.info(logger, "getPrjPrdPaySchDById", "获取PrjPrdPaySchD数据成功,ID:" + id, CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.info(logger, "getPrjPrdPaySchDById", "获取PrjPrdPaySchD数据失败,ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
            BaseLogUtils.error(logger, "getPrjPrdPaySchDById", "获取PrjPrdPaySchD数据失败,系统错误.ID:" + id + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 根据客户ID查询未核销完的支付表明细列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/searchByCstId.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object searchPayListByCstId(HttpServletRequest request, HttpServletResponse response) {
        PrjPrdPaySchDQueryCondition qc = new PrjPrdPaySchDQueryCondition();
        //设置查询条件？
        String cstId = request.getParameter("cstId");
        if (StringUtils.isNotEmpty(cstId)) {
            qc.setCstId(Long.valueOf(cstId));
        }else{
            BaseLogUtils.info(logger,"searchPayListByCstId","根据客户ID查询未核销完的支付表明细列表Error:客户ID为空");
            return null;
        }
        qc.setArAmtMin(0l);//最小剩余金额
        qc.setExcludeFeeTyp("'S01','S04'");//排除本金、剩余本金
        qc.setSortName("PAY_SCH_ID,PAY_PD");//排序字段
        qc.setSortOrder("ASC");//排序方式

        BaseLogUtils.info(logger, "searchPayListByCstId", "根据客户ID查询未核销完的支付表明细列表.DATA:" + JSONObject.toJSONString(qc), CurrentThreadContext.getCurrentUserName());
        List<PrjPrdPaySchDVO> list = this.prjPrdPaySchDService.searchList(qc);
        if(CollectionUtils.isNotEmpty(list)){
            Map<String,String> feeTypCdMap=this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(), PrjPrdPaySchDConstant.CODE_FEE_TYP_CD);
            if(feeTypCdMap!=null&&feeTypCdMap.size()>0){
                for(PrjPrdPaySchDVO vo:list){
                    if(feeTypCdMap.containsKey(vo.getFeeTypCd())){
                        vo.setFeeTypCdNm(feeTypCdMap.get(vo.getFeeTypCd()));
                    }
                }
            }
        }
        ListPageVO<PrjPrdPaySchDVO> listPage = new ListPageVO<PrjPrdPaySchDVO>(list, list.size());
        return listPage;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addPrjPrdPaySchD(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        PrjPrdPaySchDVO entity = null;//获取参数
        try {
            entity = this.getRequestParamForPrjPrdPaySchD(request);//获取参数
            Long id = this.prjPrdPaySchDService.add(entity);
            result.setData(id);
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据成功,ID:" + id + ".DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }
    
    
    /**
     * 修改结清费用金额
     */
    @RequestMapping(value = "/modifySettAmt.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modifySettAmt(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        List<PrjPrdPaySchDVO> entityList = null;//获取参数
        try {
        	String totalSetAmt = request.getParameter("stlAmt");//结清金额
        	String id = request.getParameter("id");
        	Long setAmt = 0l;
        	if(StringUtils.isNotBlank(totalSetAmt)){
        		setAmt = DecimalUtils.convertYuanToFen(Double.valueOf(totalSetAmt));
        	}
            this.prjPrdPaySchDService.modifySettAmt(setAmt,Long.valueOf(id));
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据成功, .DATA:" + JSONObject.toJSONString(entityList), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 修改结清费用项(增，删，改)
     * @param request
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/addOrRemoveSettFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addOrRemoveSettFee(HttpServletRequest request) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        String paySchIdStr = request.getParameter("paySchId");
        try {
            if (StringUtils.isEmpty(paySchIdStr)) {
                throw new BaseException("支付表ID不能为空！");
            }
            String settFeeList = request.getParameter("settFeeList");
            if (StringUtils.isEmpty(settFeeList)) {
                throw new BaseException("更新或删除的费用项数据不能为空！");
            }
            List<JSONObject> jsonList = JSON.parseArray(settFeeList, JSONObject.class);
            List<PrjPrdPaySchDDTO> adOrRmSchDList = Lists.newArrayList();
            Long paySchId = Long.parseLong(paySchIdStr);
            for (JSONObject settFeeJson : jsonList) {
                String status = settFeeJson.getString("__status");
                Long id = settFeeJson.getLong("id");
                if ("delete".equals(status) && id == null) {
                    continue;
                }
                settFeeJson.remove("__id");// 删除此key，防止下面一行代码报错
                PrjPrdPaySchDDTO paySchDDTO = JSONObject.toJavaObject(settFeeJson, PrjPrdPaySchDDTO.class);
                paySchDDTO.setPaySchId(paySchId);
                if (StringUtils.isNotEmpty(settFeeJson.getString("feeTypCdId"))) {
                    paySchDDTO.setFeeTypCd(settFeeJson.getString("feeTypCdId"));
                }
                if ("delete".equals(status)) {
                    // 费用分类- 借款类型
                    if (PrjQuatPrpsConstant.CODE_FIN_CLS_TYP_SETT.equals(paySchDDTO.getFeeClsCd())) {
                        continue;
                    }
                    // 删除标识
                    paySchDDTO.setOptFlag(PrjPrdPaySchDDTO.OptFlag.DELETED);
                } else {
                    if (StringUtils.isEmpty(paySchDDTO.getFeeTypCd())) {
                        throw new BaseException("费用类型不能为空！");
                    }
                    if (paySchDDTO.getFeeAmt() == null) {
                        throw new BaseException("费用金额不能为空！");
                    }
                    if (id == null) {
                        // 新增
                        paySchDDTO.setArAmt(DecimalUtils.convertYuanToFen(paySchDDTO.getFeeAmt()));
                        paySchDDTO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
                        paySchDDTO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
                        paySchDDTO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
                        paySchDDTO.setIsDel(BaseConstant.IS_YESNO_NO);
                        paySchDDTO.setOptFlag(PrjPrdPaySchDDTO.OptFlag.ADDED);// 新增标识
                        paySchDDTO.setCrtTm(new Date());
                        paySchDDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
                    } else {
                        // 费用分类- 借款类型
                        if (PrjQuatPrpsConstant.CODE_FIN_CLS_TYP_SETT.equals(paySchDDTO.getFeeClsCd())) {
                            continue;
                        }
                        paySchDDTO.setOptFlag(PrjPrdPaySchDDTO.OptFlag.UPDATED);// 修改标识
                    }
                    paySchDDTO.setFeeAmt(DecimalUtils.convertYuanToFen(paySchDDTO.getFeeAmt()).doubleValue());
                    if (paySchDDTO.getArAmt() != null) {
                        paySchDDTO.setArAmt(DecimalUtils.convertYuanToFen(paySchDDTO.getArAmt().doubleValue()));
                    }
                    if (paySchDDTO.getPaidUpAmt() != null) {
                        paySchDDTO.setPaidUpAmt(DecimalUtils.convertYuanToFen(paySchDDTO.getPaidUpAmt().doubleValue()));
                    }
                    if (null != paySchDDTO.getIsSysDef() && paySchDDTO.getIsSysDef() == BaseConstant.IS_YESNO_YES) {
                        paySchDDTO.setIsModify(BaseConstant.IS_YESNO_NO);
                    } else {
                        paySchDDTO.setIsSysDef(BaseConstant.IS_YESNO_NO);
                        paySchDDTO.setIsModify(BaseConstant.IS_YESNO_YES);
                    }
                    paySchDDTO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);

                }
                paySchDDTO.setMdfTm(new Date());
                paySchDDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
                adOrRmSchDList.add(paySchDDTO);
            }
            boolean success = prjPrdPaySchDService.addOrRemoveSettFeeList(paySchId, adOrRmSchDList);
            if (!success) {
                throw new BaseException("修改费用项数据失败！");
            }
            // 重新查询结清金额
            PrjPrdPaySchMVO prjPrdPaySchMVO = prjPrdPaySchMService.selectById(paySchId);
            if (prjPrdPaySchMVO.getStlAmt() != null) {
                result.setData(DecimalUtils.convertFenToYuan(prjPrdPaySchMVO.getStlAmt()));
            }
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.newLogger("新增或删除结清费用项业务异常！cause：").setBussinessId(paySchIdStr).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.newLogger("新增或删除结清费用项系统异常！cause：").setBussinessId(paySchIdStr).setThrowable(ex).error(LogExceptionEnum.API_CODE_0102);
        }
        return result;
    }

    /**
     * 删除结清费用项
     * @param request
     * @param response
     * @return
     * @throws BaseException
     */
    @RequestMapping(value = "/removeSettFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object removeSettFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
        String paySchId = request.getParameter("paySchId");
        try {
            String ids = request.getParameter("ids");
            if (StringUtils.isEmpty(ids)) {
                throw new BaseException("删除费用项数据,主键参数为空!");
            }
            List<Long> idsList = Lists.newArrayList();
            JSONArray idsArray = JSONArray.fromObject(ids);
            for (Object object : idsArray) {
                idsList.add(Long.valueOf(object.toString()));
            }
            Long stlAmt = prjPrdPaySchDService.removeSettFeeList(Long.parseLong(paySchId), idsList);
            result.setData(DecimalUtils.convertFenToYuan(stlAmt));
        } catch (BaseException e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + e.getMessage());
            BaseLogUtils.newLogger(CurrentThreadContext.getCurrentUserName() + "删除结清费用项业务逻辑异常：").setBussinessId(paySchId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        } catch (Exception e) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + e.getMessage());
            BaseLogUtils.newLogger(CurrentThreadContext.getCurrentUserName() + "删除结清费用项系统异常：").setBussinessId(paySchId).setThrowable(e).error(LogExceptionEnum.COMMON_CODE_0002);
        }
        return result;
    }

    /**
     * 新增结清费用项
     */
    @RequestMapping(value = "/addSettFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addSettFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        List<PrjPrdPaySchDVO> entityList = null;//获取参数
        try {
        	entityList = this.getRequestParamForSettFeeList(request);//获取参数
        	String totalSetAmt = request.getParameter("totalSettAmt");//结清金额
        	String paySchId = request.getParameter("paySchId");
        	Long setAmt = 0l;
        	if(StringUtils.isNotBlank(totalSetAmt)){
        		setAmt = DecimalUtils.convertYuanToFen(Double.valueOf(totalSetAmt));
        	}
            Long stlAmt = this.prjPrdPaySchDService.addSettFeeList(entityList, setAmt, Long.valueOf(paySchId));
            result.setData(DecimalUtils.convertFenToYuan(stlAmt));
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据成功, .DATA:" + JSONObject.toJSONString(entityList), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    private List<PrjPrdPaySchDVO> getRequestParamForSettFeeList(HttpServletRequest request) {
    	List<PrjPrdPaySchDVO> feeList =  new ArrayList<PrjPrdPaySchDVO>();
    	String settFeeList = request.getParameter("settFeeList");//新添加的结清费用项
    	String paySchId = request.getParameter("paySchId");//支付表ID
    	String settFlag = request.getParameter("settFlag");
    	String discountAmt = request.getParameter("discountAmt");
    	String breachAmt = request.getParameter("breachAmt");//违约金
    	String extContAmt = request.getParameter("extContAmt");//合同外金额
    	String settPct = request.getParameter("settPct");//结清比例
    	String reduAmt = request.getParameter("reduAmt");//优惠金额
    	String yzdgFlag = request.getParameter("yzdgFlag");//以租代购提前结清标识
    	String prjId = request.getParameter("prjId");//项目id
    	String reduStartLsPd = request.getParameter("reduStartLsPd");//优惠开始期数 
    	PrjBscInfoVO prjBscInfoVO = this.prjBscInfoService.selectById(Long.valueOf(prjId));
    	boolean yzdgFeeFlag = false;//是否已经添加了一组代购的优惠比例费用项
		if (StringUtils.isNotBlank(settFeeList)) {
			feeList = JSON.parseArray(settFeeList, PrjPrdPaySchDVO.class);
			for (PrjPrdPaySchDVO prjPrdPaySchDVO : feeList) {
				if(StringUtils.isNotBlank(yzdgFlag)&&yzdgFlag.equals(String.valueOf(BaseConstant.IS_YESNO_YES))){
					if(prjPrdPaySchDVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.FEE_TYP_CD_300)||prjPrdPaySchDVO.getFeeTypCd().equals(PrjQuatPrpsFeeConstant.FEE_TYP_CD_301)){
						yzdgFeeFlag = true;
					}
				}
				
				if(StringUtils.isNotBlank(paySchId)){
					prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
				}
				if(StringUtils.isBlank(prjPrdPaySchDVO.getFeeClsCd())){
					prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
				}
				if(null==prjPrdPaySchDVO.getFeeAmt()){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项金额不能为空!");
                }
				if(null==prjPrdPaySchDVO.getFeeTypCd()||StringUtils.isBlank(prjPrdPaySchDVO.getFeeTypCd())){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项类型不能为空!");
                }

				prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(prjPrdPaySchDVO.getFeeAmt()).doubleValue());
				/*if(null==prjPrdPaySchDVO.getArAmt()){
					prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());
				}*/

                //费用金额不对修改
                prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());

				prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setMdfTm(new Date());
				prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setCrtTm(new Date());
				prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
				if(null!=prjPrdPaySchDVO.getIsSysDef()&&prjPrdPaySchDVO.getIsSysDef()==BaseConstant.IS_YESNO_YES){
					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_NO);
				}else{
					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_NO);
					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_YES);
				}
			}
			//提前结清加入默认的优惠费用项
			if(StringUtils.isNotBlank(settFlag)&&settFlag.equals(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_PRE)&&StringUtils.isNotBlank(discountAmt)&&StringUtils.isNotBlank(breachAmt)){
				//提前优惠金额
				PrjPrdPaySchDVO prjPrdPaySchDDTO = new PrjPrdPaySchDVO();
				if(StringUtils.isNotBlank(yzdgFlag)&&yzdgFlag.equals(String.valueOf(BaseConstant.IS_YESNO_YES))){
					prjPrdPaySchDDTO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(reduAmt)).doubleValue());
					prjPrdPaySchDDTO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(reduAmt)));
				}else{
					prjPrdPaySchDDTO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(discountAmt)).doubleValue());
					prjPrdPaySchDDTO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(discountAmt)));
				}
				prjPrdPaySchDDTO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDDTO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
				prjPrdPaySchDDTO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDDTO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDDTO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDDTO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_201);
				prjPrdPaySchDDTO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT);
				prjPrdPaySchDDTO.setIsSysDef(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDDTO.setMdfTm(new Date());
				prjPrdPaySchDDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDDTO.setCrtTm(new Date());
				prjPrdPaySchDDTO.setPaySchId(Long.valueOf(paySchId));
				if(!prjBscInfoVO.getBuOrgCd().equals(PrjBscInfoConstant.CODE_BU_ORG_CD_ZC)){
					//违约金
					PrjPrdPaySchDVO prjPrdPaySchDVO = new PrjPrdPaySchDVO();
					prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)).doubleValue());
					prjPrdPaySchDVO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)));
					prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
					prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
					prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
					prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
					prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
					prjPrdPaySchDVO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_103);
					prjPrdPaySchDVO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_IN);
					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
					prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
					prjPrdPaySchDVO.setMdfTm(new Date());
					prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					prjPrdPaySchDVO.setCrtTm(new Date());
					prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
					feeList.add(prjPrdPaySchDVO);
				}
				
				
				feeList.add(prjPrdPaySchDDTO);
			}
			//不存在添加费用项
			if(StringUtils.isNotBlank(yzdgFlag)&&yzdgFeeFlag==false&&settFlag.equals(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_PRE)&&prjBscInfoVO.getBuOrgCd().equals(PrjBscInfoConstant.CODE_BU_ORG_CD_ZC)){
				//优惠外合同金额
				PrjPrdPaySchDVO prjPrdPaySchDDTO = new PrjPrdPaySchDVO();
				prjPrdPaySchDDTO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(extContAmt)).doubleValue());
				prjPrdPaySchDDTO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(extContAmt)));
				prjPrdPaySchDDTO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDDTO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_YZDG);
				prjPrdPaySchDDTO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDDTO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDDTO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDDTO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_301);
				prjPrdPaySchDDTO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT);
				prjPrdPaySchDDTO.setIsSysDef(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDDTO.setMdfTm(new Date());
				prjPrdPaySchDDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDDTO.setCrtTm(new Date());
				prjPrdPaySchDDTO.setPaySchId(Long.valueOf(paySchId));
				
				//优惠比例
				PrjPrdPaySchDVO prjPrdPaySchDVO = new PrjPrdPaySchDVO();
				prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)).doubleValue());
				prjPrdPaySchDVO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)));
				prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_YZDG);
				prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDVO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_300);
				prjPrdPaySchDVO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT);
				prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDVO.setFeePct(Double.valueOf(settPct));
				prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setMdfTm(new Date());
				prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setCrtTm(new Date());
				prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
				
				//优惠开始期数
				PrjPrdPaySchDVO prjPrdPaySchPdDVO = new PrjPrdPaySchDVO();
				prjPrdPaySchPdDVO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)).doubleValue());
				prjPrdPaySchPdDVO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)));
				prjPrdPaySchPdDVO.setIsFixedAmt(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchPdDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_YZDG);
				prjPrdPaySchPdDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchPdDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchPdDVO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchPdDVO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_302);
				prjPrdPaySchPdDVO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_OUT);
				prjPrdPaySchPdDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchPdDVO.setFeePct(Double.valueOf(reduStartLsPd));
				prjPrdPaySchPdDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchPdDVO.setMdfTm(new Date());
				prjPrdPaySchPdDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchPdDVO.setCrtTm(new Date());
				prjPrdPaySchPdDVO.setPaySchId(Long.valueOf(paySchId));
				feeList.add(prjPrdPaySchPdDVO);
				feeList.add(prjPrdPaySchDVO);
				feeList.add(prjPrdPaySchDDTO);
				
			}
			
		}
		
		
		return feeList;
	}

	/**
     * 修改
     */
    @RequestMapping(value = "/modify.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modifyPrjPrdPaySchD(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
        PrjPrdPaySchDVO entity = null;
        try {
            entity = this.getRequestParamForPrjPrdPaySchD(request);
            if (entity.getId() == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
                BaseLogUtils.info(logger, "modifyPrjPrdPaySchD", "修改PrjPrdPaySchD信息,主键参数为空！.", CurrentThreadContext.getCurrentUserName());
                return result;
            }

            boolean success = this.prjPrdPaySchDService.modify(entity);
            if (!success) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
                BaseLogUtils.info(logger, "modifyPrjPrdPaySchD", "修改PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
                return result;
            }
            BaseLogUtils.info(logger, "modifyPrjPrdPaySchD", "修改PrjPrdPaySchD数据成功.DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "modifyPrjPrdPaySchD", "修改PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "modifyPrjPrdPaySchD", "修改PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

    /**
     * 获取请求参数
     *
     * @param request 请求
     * @return
     */
    private PrjPrdPaySchDVO getRequestParamForPrjPrdPaySchD(HttpServletRequest request) throws BaseException {
        //TODO 获取参数
        String id = request.getParameter("id");//主键
        String paySchId = request.getParameter("paySchId");
        String payPd = request.getParameter("payPd");
        String payDt = request.getParameter("payDt");
        String feeTypCd = request.getParameter("feeTypCd");
        String calBaseCd = request.getParameter("calBaseCd");
        String feePct = request.getParameter("feePct");
        String feeAmt = request.getParameter("feeAmt");
        String cntWayCd = request.getParameter("cntWayCd");
        String arAmt = request.getParameter("arAmt");
        String paidUpAmt = request.getParameter("paidUpAmt");


        //验证参数并赋值
        PrjPrdPaySchDVO entity = new PrjPrdPaySchDVO();
        Date tm = new Date();
        if (StringUtils.isNotBlank(id)) {
            entity.setId(Long.valueOf(id));
            entity.setMdfTm(tm);
            entity.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
        } else {
            entity.setCrtTm(tm);
            entity.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
        }

        return entity;
    }
    
    
    /**
     * 新增乘用车结清费用项
     */
    @RequestMapping(value = "/addCarSettFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addCarSettFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        List<PrjPrdPaySchDVO> entityList = null;//获取参数
        try {
        	entityList = this.getRequestParamForCarSettFeeList(request);//获取参数
        	String totalSetAmt = request.getParameter("totalSettAmt");//结清金额
        	String paySchId = request.getParameter("paySchId");
        	String settFlag = request.getParameter("settFlag");//提前结清还是正常结清
        	Long setAmt = 0l;
        	boolean flag = true;
        	if(StringUtils.isNotBlank(totalSetAmt)){
        		setAmt = DecimalUtils.convertYuanToFen(Double.valueOf(totalSetAmt));
        	}
            this.prjPrdPaySchDService.addSettFeeList(entityList,setAmt,Long.valueOf(paySchId));
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据成功, .DATA:" + JSONObject.toJSONString(entityList), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(ex.getMessage());
            BaseLogUtils.error(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

	private List<PrjPrdPaySchDVO> getRequestParamForCarSettFeeList(HttpServletRequest request) {
		List<PrjPrdPaySchDVO> feeList =  new ArrayList<PrjPrdPaySchDVO>();
    	String settFeeList = request.getParameter("settFeeList");//新添加的结清费用项
    	String paySchId = request.getParameter("paySchId");//支付表ID
    	String settFlag = request.getParameter("settFlag");
    	String breachAmt = request.getParameter("breachAmt");//违约金
		if (StringUtils.isNotBlank(settFeeList)) {
			feeList = JSON.parseArray(settFeeList, PrjPrdPaySchDVO.class);
			for (PrjPrdPaySchDVO prjPrdPaySchDVO : feeList) {
				if(StringUtils.isNotBlank(paySchId)){
					prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
				}
				prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
				if(null==prjPrdPaySchDVO.getFeeAmt()){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项金额不能为空!");
                }
				if(null==prjPrdPaySchDVO.getFeeTypCd()||StringUtils.isBlank(prjPrdPaySchDVO.getFeeTypCd())){
					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项类型不能为空!");
                }

				prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(prjPrdPaySchDVO.getFeeAmt()).doubleValue());
				/*if(null==prjPrdPaySchDVO.getArAmt()){
					prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());
				}*/

                //费用金额不对修改
                prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());

				prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setMdfTm(new Date());
				prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setCrtTm(new Date());
				prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
				if(null!=prjPrdPaySchDVO.getIsSysDef()&&prjPrdPaySchDVO.getIsSysDef()==BaseConstant.IS_YESNO_YES){
					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_NO);
				}else{
					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_NO);
					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_YES);
				}
			}
			//提前结清加入默认的优惠费用项
			if(StringUtils.isNotBlank(settFlag)&&settFlag.equals(PrjPrdPaySchMConstant.PRJ_PRD_Pay_SCH_M_PRE)&&StringUtils.isNotBlank(breachAmt)){
				//违约金
				PrjPrdPaySchDVO prjPrdPaySchDVO = new PrjPrdPaySchDVO();
				prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)).doubleValue());
				prjPrdPaySchDVO.setArAmt(DecimalUtils.convertYuanToFen(Double.valueOf(breachAmt)));
				prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
				prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
				prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
				prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
				prjPrdPaySchDVO.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_103);
				prjPrdPaySchDVO.setCapFlow(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CASH_FLOW_IN);
				prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
				prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setMdfTm(new Date());
				prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				prjPrdPaySchDVO.setCrtTm(new Date());
				prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
				feeList.add(prjPrdPaySchDVO);
			}
		}
		return feeList;
	}
	
	/**
     * 修改提前还清期次
     */
    @RequestMapping(value = "/modifyPayPd.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object modifyPayPd(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
        String id = request.getParameter("id");
        String payPd = request.getParameter("payPd");
        String lsPdMax = request.getParameter("lsPdMax");
        List<PrjPrdPaySchDVO> entity = null;
        try {
            if (id == null) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
                BaseLogUtils.info(logger, "modifyPrjPrdPaySchM", "修改PrjPrdPaySchM信息,主键参数为空！.", CurrentThreadContext.getCurrentUserName());
                return result;
            }
            PrjPrdPaySchDQueryCondition condition = new PrjPrdPaySchDQueryCondition();
            condition.setPaySchId(Long.valueOf(id));
            condition.setPayPd(Long.valueOf(payPd));
            entity = this.prjPrdPaySchDService.searchListByPayPd(condition);
            
            Double feeAmtS01 = 0d;//费用金额
            Long arAmtS01 = 0l;//未核销金额
            Long paidUpAmtS01 = 0l;//已核销金额
            
            Double feeAmtS02 = 0d;//费用金额
            Long arAmtS02 = 0l;//未核销金额
            Long paidUpAmtS02 = 0l;//已核销金额
            
            Double feeAmtS03 = 0d;//费用金额
            Long arAmtS03 = 0l;//未核销金额
            Long paidUpAmtS03 = 0l;//已核销金额
            
            PrjPrdPaySchDVO prjPrdPaySchDVOS01 = new PrjPrdPaySchDVO();//租金
            PrjPrdPaySchDVO prjPrdPaySchDVOS02 = new PrjPrdPaySchDVO();//利息
            PrjPrdPaySchDVO prjPrdPaySchDVOS03 = new PrjPrdPaySchDVO();//本金
            PrjPrdPaySchDVO prjPrdPaySchDVOS04 = new PrjPrdPaySchDVO();//剩余本金
            
            if(CollectionUtils.isNotEmpty(entity)){
            	boolean flag = true;
            	for(PrjPrdPaySchDVO vo :entity){
            		if((vo.getPayPd()+"").equals(lsPdMax)){
            			flag = false;
            			break;
            		}
            	}
            	if(flag){
            		result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("已经过提前结清处理！");
            	}else{
            		for(PrjPrdPaySchDVO vo :entity){
                		if(vo.getFeeTypCd().equals("S01")){
                			if((vo.getPayPd()+"").equals(payPd)){
                				prjPrdPaySchDVOS01 = vo;
                			}
                			feeAmtS01 += vo.getFeeAmt();
                			arAmtS01 += vo.getArAmt();
                			paidUpAmtS01 += vo.getPaidUpAmt();
                		}
                		if(vo.getFeeTypCd().equals("S02")){
                			if((vo.getPayPd()+"").equals(payPd)){
                				prjPrdPaySchDVOS02 = vo;
                			}
                			feeAmtS02 += vo.getFeeAmt();
                			arAmtS02 += vo.getArAmt();
                			paidUpAmtS02 += vo.getPaidUpAmt();
                		}
                		if(vo.getFeeTypCd().equals("S03")){
                			if((vo.getPayPd()+"").equals(payPd)){
                				prjPrdPaySchDVOS03 = vo;
                			}
                			feeAmtS03 += vo.getFeeAmt();
                			arAmtS03 += vo.getArAmt();
                			paidUpAmtS03 += vo.getPaidUpAmt();
                		}
                		if(vo.getFeeTypCd().equals("S04")){
                			if((vo.getPayPd()+"").equals(payPd)){
                				prjPrdPaySchDVOS04 = vo;
                			}
                		}
                		vo.setIsDel(1);
                		this.prjPrdPaySchDService.modify(vo);
                	}
                	prjPrdPaySchDVOS01.setFeeAmt(feeAmtS01);
                	prjPrdPaySchDVOS01.setArAmt(arAmtS01);
                	prjPrdPaySchDVOS01.setPaidUpAmt(paidUpAmtS01);
                	prjPrdPaySchDVOS01.setIsDel(0);
                	prjPrdPaySchDVOS02.setFeeAmt(feeAmtS02);
                	prjPrdPaySchDVOS02.setArAmt(arAmtS02);
                	prjPrdPaySchDVOS02.setPaidUpAmt(paidUpAmtS02);
                	prjPrdPaySchDVOS02.setIsDel(0);
                	prjPrdPaySchDVOS03.setFeeAmt(feeAmtS03);
                	prjPrdPaySchDVOS03.setArAmt(arAmtS03);
                	prjPrdPaySchDVOS03.setPaidUpAmt(paidUpAmtS03);
                	prjPrdPaySchDVOS03.setIsDel(0);
                	prjPrdPaySchDVOS04.setFeeAmt(0d);
                	prjPrdPaySchDVOS04.setArAmt(0l);
                	prjPrdPaySchDVOS04.setIsDel(0);
                	
                	this.prjPrdPaySchDService.add(prjPrdPaySchDVOS01);
                	this.prjPrdPaySchDService.add(prjPrdPaySchDVOS02);
                	this.prjPrdPaySchDService.add(prjPrdPaySchDVOS03);
                	this.prjPrdPaySchDService.add(prjPrdPaySchDVOS04);
            	}
            }else{
            	result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("已经过提前结清处理!");
            }
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "modifyPrjPrdPaySchM", "修改PrjPrdPaySchM数据失败.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "modifyPrjPrdPaySchM", "修改PrjPrdPaySchM数据失败,系统错误.DATA:" + JSONObject.toJSONString(entity) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }
    /**
     * 新增结清费用项
     */
    @RequestMapping(value = "/addPrjPrdPaySettFee.do", method = {RequestMethod.POST})
    @ResponseBody
    public Object addPrjPrdPaySettFee(HttpServletRequest request, HttpServletResponse response) throws BaseException {
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
        List<PrjPrdPaySchDVO> entityList = null;//获取参数
        try {
        	List<PrjPrdPaySchDVO> feeList =  new ArrayList<PrjPrdPaySchDVO>();
        	String settFeeList = request.getParameter("settFeeList");//新添加的结清费用项
        	String paySchId = request.getParameter("paySchId");//支付表ID
    		if (StringUtils.isNotBlank(settFeeList)) {
    			feeList = JSON.parseArray(settFeeList, PrjPrdPaySchDVO.class);
    			for (PrjPrdPaySchDVO prjPrdPaySchDVO : feeList) {
    				if(StringUtils.isNotBlank(paySchId)){
    					prjPrdPaySchDVO.setPaySchId(Long.valueOf(paySchId));
    				}
    				if(StringUtils.isBlank(prjPrdPaySchDVO.getFeeClsCd())){
    					prjPrdPaySchDVO.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
    				}
    				if(null==prjPrdPaySchDVO.getFeeAmt()){
    					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项金额不能为空!");
                    }
    				if(null==prjPrdPaySchDVO.getFeeTypCd()||StringUtils.isBlank(prjPrdPaySchDVO.getFeeTypCd())){
    					throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,"费用项类型不能为空!");
                    }

    				prjPrdPaySchDVO.setFeeAmt(DecimalUtils.convertYuanToFen(prjPrdPaySchDVO.getFeeAmt()).doubleValue());
    				/*if(null==prjPrdPaySchDVO.getArAmt()){
    					prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());
    				}*/

                    //费用金额不对修改
                    prjPrdPaySchDVO.setArAmt(prjPrdPaySchDVO.getFeeAmt().longValue());

    				prjPrdPaySchDVO.setPayPd(PrjQuatPrpsConstant.CODE_PRJ_PRD_PAY_PD);
    				prjPrdPaySchDVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
    				prjPrdPaySchDVO.setMdfTm(new Date());
    				prjPrdPaySchDVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
    				prjPrdPaySchDVO.setCrtTm(new Date());
    				prjPrdPaySchDVO.setCntWayCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_CNT_WAY_OTH);
    				prjPrdPaySchDVO.setIsDel(BaseConstant.IS_YESNO_NO);
    				prjPrdPaySchDVO.setIsFixedAmt(BaseConstant.IS_YESNO_YES);
    				if(null!=prjPrdPaySchDVO.getIsSysDef()&&prjPrdPaySchDVO.getIsSysDef()==BaseConstant.IS_YESNO_YES){
    					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_YES);
    					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_NO);
    				}else{
    					prjPrdPaySchDVO.setIsSysDef(BaseConstant.IS_YESNO_NO);
    					prjPrdPaySchDVO.setIsModify(BaseConstant.IS_YESNO_YES);
    				}
    			}
    			
    		}
    		
    		
            this.prjPrdPaySchDService.addSettFeeList(feeList);
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据成功, .DATA:" + JSONObject.toJSONString(entityList), CurrentThreadContext.getCurrentUserName());
        } catch (BaseException ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.info(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        } catch (Exception ex) {
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR + ex.getMessage());
            BaseLogUtils.error(logger, "addPrjPrdPaySchD", "新增PrjPrdPaySchD数据失败,系统错误.DATA:" + JSONObject.toJSONString(entityList) + ".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
        }
        return result;
    }

}

